#include "Stack.h"


void STInit(ST* s)
{
	assert(s);
	STDataType* tmp = (STDataType*)malloc(sizeof(STDataType) * INITCAPACITY);
	if (tmp == NULL)
	{
		perror("malloc fail");
		return;
	}
	else
	{
		s->a = tmp;
		s->capacity = INITCAPACITY;
		s->top = 0;
		//top等于0，指的是栈顶元素的下一位置
	    //s->top = -1;   //指的是栈顶元素的位置
	}
}

void STPush(ST* s, STDataType x)
{
	assert(s);
	if (s->top == s->capacity)
	{
		STDataType* tmp = (STDataType*)realloc(s->a,sizeof(STDataType) * s->capacity *2);
		if (tmp == NULL)
		{
			perror("malloc fail");
			return;
		}
		else
		{
			s->a = tmp;
			s->capacity *= 2;
		}

	}

	s->a[s->top] = x;
	s->top++;
}

void STPop(ST* s)
{
	assert(s);
	assert(!STEmpy(s));
	s->top--;
}

void STDestroy(ST* s)
{
	assert(s);
	free(s->a);
	s->top = 0;
	s->capacity = 0;
	s = NULL;
}

STDataType STTop(ST* s)
{
	assert(s);
	assert(!STEmpy(s));
	return s->a[s->top - 1];
}


bool STEmpy(ST* s)
{
	assert(s);
	return s->top == 0;
}